package writer_;

import org.junit.jupiter.api.Test;

import java.io.FileWriter;
import java.io.IOException;

/**
 * @author DCSGO
 * @version 1.0
 */

/*
FileWriter常用方法
1) new FileWriter(File/String):覆盖模式，相当于流的指针在首端
2) new FileWriter(File/String,true):追加模式，相当于流的指针在尾端
3) write(int):写入单个字符
4) write(char[]):写入指定数组
5) write(char[],off,len):写入指定数组的指定部分
6) write(string):写入整个字符串
7) write(string,off,len):写入字符串的指定部分
相关API: String类:toCharArray:将String转换成char[]
注意:
FileWriter使用后，必须要关闭(close)或刷新(flush)，否则写入不到指定的文件!
 */

public class FileWriter_ {
    @Test
    public void m() {
        String filePath = "C:\\Users\\Administrator\\Desktop\\Javacode\\java-learning-code\\chapter19\\src\\writer_\\note.txt";
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(filePath);
            fileWriter.write('W');
            fileWriter.write(new char[]{'h', 'y'});
            fileWriter.write(new char[]{' ', 'a', 'r', 'e', ' '}, 0, 4);
            fileWriter.write(" you");
            fileWriter.write("Why are you cry? --汤姆", 11, 10);
            //文件最后内容：Why are you cry? --汤姆

            System.out.println("写入成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileWriter != null) {
                    //close => flush + 关闭
                    fileWriter.close();
                }
            } catch (IOException e) {
                e.getStackTrace();
            }
        }
        System.out.println("程序结束");
    }

    /**
     * 使用FileWriter将
     * "风雨之后，定见彩虹"
     * 写入到note.txt文件中
     */
    @Test
    public void writer1() {
        String filePath = "C:\\Users\\Administrator\\Desktop\\Javacode\\java-learning-code\\chapter19\\src\\writer_\\note.txt";
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(filePath);
            String date = "风雨之后，定见彩虹";
            char[] write = date.toCharArray();
            for (char ch : write) {
                fileWriter.write(ch);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileWriter != null) {
                    //close => flush + 关闭
                    fileWriter.close();
                }
            } catch (IOException e) {
                e.getStackTrace();
            }
        }
    }

    @Test
    public void writer2() {
        String filePath = "C:\\Users\\Administrator\\Desktop\\Javacode\\java-learning-code\\chapter19\\src\\writer_\\note.txt";
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(filePath);
            String date = "风雨之后，定见彩虹";
            //char[] write = date.toCharArray();
            fileWriter.write(date);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileWriter != null) {
                    fileWriter.close();
                }
            } catch (IOException e) {
                e.getStackTrace();
            }
        }
    }
}
